package com.remoter.basic.registry.support;

import java.io.IOException;
import java.util.concurrent.CopyOnWriteArrayList;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.remoter.basic.bean.support.ConsumerModule;
import com.remoter.basic.bean.support.ProviderModule;
import com.remoter.basic.registry.IRegistry;
import com.remoter.basic.registry.IRegistryConsumerListener;
import com.remoter.basic.registry.IRegistryProviderListener;

/**
 * @author			koko
 * @date 			2017-08-08 14:51:40
 * @description 	抽出注册中心常用功能
 */
public abstract class AbstractRegistry implements IRegistry{
	
	protected static final Logger logger = LoggerFactory.getLogger(AbstractRegistry.class);
	private static final CopyOnWriteArrayList<IRegistryProviderListener> providerListeners = new CopyOnWriteArrayList<IRegistryProviderListener>();
	private static final CopyOnWriteArrayList<IRegistryConsumerListener> consumerListeners = new CopyOnWriteArrayList<IRegistryConsumerListener>();
	
	@Override
	public boolean attachProviderListener(IRegistryProviderListener registryProviderListener){
		if(null == registryProviderListener || providerListeners.contains(registryProviderListener)){
			return false;
		}
		providerListeners.add(registryProviderListener);
		return true;
	}
	
	@Override
	public boolean attachConsumerListener(IRegistryConsumerListener registryConsumerListener){
		if(null == registryConsumerListener || consumerListeners.contains(registryConsumerListener)){
			return false;
		}
		consumerListeners.add(registryConsumerListener);
		return true;
	}

	@Override
	public void close() throws IOException {
		providerListeners.clear();
		consumerListeners.clear();
	}
	
	protected void fireProviderCreate(String path,ProviderModule providerModule){
		for(IRegistryProviderListener registryProviderListener : providerListeners){
			registryProviderListener.onCreate(path,providerModule);
		}
	}
	protected void fireProviderDelete(String path,ProviderModule providerModule){
		for(IRegistryProviderListener registryProviderListener : providerListeners){
			registryProviderListener.onDelete(path,providerModule);
		}
	}
	protected void fireProviderUpdate(String path,ProviderModule providerModule){
		for(IRegistryProviderListener registryProviderListener : providerListeners){
			registryProviderListener.onUpdate(path,providerModule);
		}
	}
	
	protected void fireConsumerCreate(String path,ConsumerModule consumerModule){
		for(IRegistryConsumerListener registryConsumerListener : consumerListeners){
			registryConsumerListener.onCreate(path,consumerModule);
		}
	}
	protected void fireConsumerDelete(String path,ConsumerModule consumerModule){
		for(IRegistryConsumerListener registryConsumerListener : consumerListeners){
			registryConsumerListener.onDelete(path,consumerModule);
		}
	}
	protected void fireConsumerUpdate(String path,ConsumerModule consumerModule){
		for(IRegistryConsumerListener registryConsumerListener : consumerListeners){
			registryConsumerListener.onUpdate(path,consumerModule);
		}
	}
	
}